//
//  SACollectionPayView.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/8/30.
//

#import "SACollectionPayView.h"
#import "UILabel+NSMutableAttributedString.h"
#import "UIButton+LPCustomBtn.h"

@interface SACollectionPayView ()

@property (nonatomic, strong) UIView *pinkView;
@property (nonatomic, strong) NSString *totalPrice;
@property (nonatomic, assign) BOOL isVip;
@property (nonatomic, assign) BOOL isScan;//扫码收款
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIButton *settlementBtn;

@property (nonatomic, strong) NSString *defaultPayType;

@end

@implementation SACollectionPayView

-(instancetype)initWithTotalPrice:(NSString *)totalPrice AndIsVip:(BOOL)isVip IsScan:(BOOL)scan PayType:(NSString *)payType{
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]) {
        self.totalPrice = totalPrice;
        self.isVip = isVip;
        self.defaultPayType = payType;
        
        BOOL isPayChannel = ![[SAValueManger shareInStance].userInfo.payChannel isEqualToString:@"2"];
        if (isPayChannel == YES && scan == YES) {
            self.isScan = YES;
        }else {
            self.isScan = NO;
        }

        [self initUI];
    }
    return self;
}


//防止多次结单
- (void)unisversalButtonAcrion:(UIButton *)sender
{
    sender.userInteractionEnabled = NO;
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(clickedSettlementBtn) object:sender];
    [self performSelector:@selector(clickedSettlementBtn) withObject:sender afterDelay:0.2f];
}

-(void)clickedSettlementBtn {
    !self.settlementBlock ?: self.settlementBlock([NSString stringWithFormat:@"%zd",self.selectBtn.tag]);
    [self shareDissTapEvent];
    self.settlementBtn.userInteractionEnabled  = YES;
}

-(void)clickedTap {
    !self.combinationBlock ?: self.combinationBlock();
    [self shareDissTapEvent];
}

-(void)clickedDeleteBtn {
    [self shareDissTapEvent];
}

-(void)clickedPayBtn:(UIButton *)sender {
    CGFloat x = (kScreenWidth - kScaleNum(280))/2;
    if (self.isScan == YES && self.isVip == YES) {
        x = (kScreenWidth - kScaleNum(360))/2;
        self.pinkView.frame = CGRectMake(sender.x, kScaleNum(40), kScaleNum(60), kScaleNum(60));
    }else if (self.isVip == YES) {
        x = (kScreenWidth - kScaleNum(350))/2;
        self.pinkView.frame = CGRectMake(sender.x + kScaleNum(5), kScaleNum(40), kScaleNum(60), kScaleNum(60));
    }else if (self.isScan == YES) {
        x = (kScreenWidth - kScaleNum(350))/2;
        self.pinkView.frame = CGRectMake(sender.x + kScaleNum(5), kScaleNum(40), kScaleNum(60), kScaleNum(60));
    }else {
        self.pinkView.frame = CGRectMake(sender.x + kScaleNum(5), kScaleNum(40), kScaleNum(60), kScaleNum(60));
    }

    self.selectBtn = sender;
}

#pragma mark - initUI
-(void)initUI {
    self.backgroundColor = kColor(0, 0, 0, 0.6);
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight -  kScaleNum(165) - SafeFooterHeight, kScreenWidth, kScaleNum(165) + SafeFooterHeight)];
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight -  kScaleNum(165) - SafeFooterHeight);
    [deleteBtn addTarget:self action:@selector(clickedDeleteBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UILabel *totalLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(20), 0, kScaleNum(150), kScaleNum(37))];
    totalLab.text = [NSString stringWithFormat:@"收款 ￥%@",self.totalPrice];
    totalLab.font = kFont(15);
    totalLab.textAlignment = NSTextAlignmentLeft;
    totalLab.textColor = kPinkColor;
    [backView addSubview:totalLab];
    [totalLab setAttributedStringWithSubString:@"收款" color:kBlackColor];
    
    UILabel *combinationLab = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(170), 0, kScaleNum(150), kScaleNum(37))];
    combinationLab.text = @"组合支付";
    combinationLab.font = kFont(15);
    combinationLab.textAlignment = NSTextAlignmentRight;
    combinationLab.textColor = kBuleColor;
    [backView addSubview:combinationLab];
    combinationLab.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedTap)];
    [combinationLab addGestureRecognizer:tap];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(37), kScreenWidth, kScaleNum(0.5))];
    line.backgroundColor = kLineColor;
    [backView addSubview:line];
    
    CGFloat x = (kScreenWidth - kScaleNum(280))/2;
    CGFloat height = kScaleNum(70);
    CGFloat width = kScaleNum(70);
    NSInteger font = 11;//字体适配
    NSInteger pinkX = 5;//pinkView 偏移
    NSArray *imageArr = @[@"cash", @"card", @"weChat", @"Alipay"];
    NSArray *titleArr = @[@"现金", @"刷卡", @"微信", @"支付宝"];
    if (self.isVip == YES && self.isScan == YES) {
        x = (kScreenWidth - kScaleNum(360))/2;
        width = kScaleNum(60);
        imageArr = @[@"pay_scan", @"cash", @"card", @"weChat", @"Alipay", @"balance"];
        titleArr = @[@"扫码收款", @"现金", @"刷卡", @"微信", @"支付宝", @"余额"];
        font = 10;
        pinkX = 0;
    }else if (self.isVip == YES) {
        x = (kScreenWidth - kScaleNum(350))/2;
        imageArr = @[@"cash", @"card", @"weChat", @"Alipay", @"balance"];
        titleArr = @[@"现金", @"刷卡", @"微信", @"支付宝", @"余额"];
    }else if (self.isScan == YES) {
        x = (kScreenWidth - kScaleNum(350))/2;
        imageArr = @[@"pay_scan", @"cash", @"card", @"weChat", @"Alipay"];
        titleArr = @[@"扫码收款", @"现金", @"刷卡", @"微信", @"支付宝"];
    }

    for (int i = 0; i < imageArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        btn.titleLabel.font = kFont(font);
        [btn addTarget:self action:@selector(clickedPayBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(x + width * i, kScaleNum(40), width, height);
        [backView addSubview:btn];
        
        if ([titleArr[i] isEqualToString:@"扫码收款"]) {
            [self setButtonMiddle:btn];
        }else {
            [self setButtonMiddle2:btn];
        }

        if (i == 0) {
            self.selectBtn = btn;
        }
        if ([titleArr[i] isEqualToString:@"扫码收款"]) {
            btn.tag = 7;
        }else if ([titleArr[i] isEqualToString:@"现金"]) {
            btn.tag = 1;
        }else if ([titleArr[i] isEqualToString:@"刷卡"]) {
            btn.tag = 2;
        }else if ([titleArr[i] isEqualToString:@"微信"]) {
            btn.tag = 4;
        }else if ([titleArr[i] isEqualToString:@"支付宝"]) {
            btn.tag = 5;
        }else if ([titleArr[i] isEqualToString:@"余额"]) {
            btn.tag = 6;
        }
        if ([self.defaultPayType integerValue] == btn.tag) {
            self.selectBtn = btn;
        }
    }
    
    UIButton *settlementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settlementBtn setTitle:@"结算" forState:UIControlStateNormal];
    [settlementBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [settlementBtn setBackgroundImage:[UIImage imageNamed:@"button_blue_bg"] forState:UIControlStateNormal];
    settlementBtn.titleLabel.font = kFont(15);
    settlementBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(120), kScreenWidth - kScaleNum(140), kScaleNum(35));
    [settlementBtn addTarget:self action:@selector(unisversalButtonAcrion:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:settlementBtn];
    self.settlementBtn = settlementBtn;
    
    UIView *pinkView = [[UIView alloc] initWithFrame:CGRectMake(x + kScaleNum(pinkX), kScaleNum(40), kScaleNum(60), kScaleNum(60))];
    pinkView.layer.borderWidth = kScaleNum(1);
    pinkView.layer.borderColor = kPinkColor.CGColor;
    pinkView.userInteractionEnabled = NO;
    [backView addSubview:pinkView];
    self.pinkView = pinkView;
    [self clickedPayBtn:self.selectBtn];
}

-(void)shareDissTapEvent {
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)setButtonMiddle:(UIButton *)btn {
    float imgW = btn.imageView.frame.size.width;
    float imgH = btn.imageView.frame.size.height;
    float lblW = btn.titleLabel.frame.size.width;
    float lblH = btn.titleLabel.frame.size.height;
    //设置图片和文字的间距，这里可自行调整
    float margin = 10.0;
    btn.imageEdgeInsets = UIEdgeInsetsMake(-lblH-margin/2, kScaleNum(12), 0, -lblW);
    btn.titleEdgeInsets = UIEdgeInsetsMake(imgH+margin/2, -imgW, 0, 0);
}

-(void)setButtonMiddle2:(UIButton *)btn {
    float imgW = btn.imageView.frame.size.width;
    float imgH = btn.imageView.frame.size.height;
    float lblW = btn.titleLabel.frame.size.width;
    float lblH = btn.titleLabel.frame.size.height;
    //设置图片和文字的间距，这里可自行调整
    float margin = 10.0;
    btn.imageEdgeInsets = UIEdgeInsetsMake(-lblH-margin/2, 0, 0, -lblW);
    btn.titleEdgeInsets = UIEdgeInsetsMake(imgH+margin/2, -imgW, 0, 0);
}

@end


